﻿// ==UserScript==
// @name         user-web-lite
// @namespace    https://user-web.googlecode.com/
// @version      0.5
// @description  insert user styles
// @match        http*://*/*
// @match        http://localhost:4444/TestLive.htm
// @match        https://localhost:44300/TestLive.htm
// @exclude      http://localhostx*
// @exclude      */owa/*
// @exclude      https://www.google.co.uk/_/chrome/newtab*
// @exclude      https://mail.google.com/tasks/*
// @exclude      http*://*.facebook.com*
// @exclude      http*://rs.gwallet.com*
// @exclude      http*://*.struq.com*
// @exclude      xhttp*://www.google.com*
// @copyright    2010+, james_zerty
// @xrequire      https://localhost:44300/Scripts/user-web-lite.js?123
// ==/UserScript==
try {
	var url = document.location.href;

    var isTop =
        (url.match(/getpocket\.com\/edit/ig) !== null) ||
        (parent && parent.location && parent.location.href == url);

	if (isTop) {
		var userWeb = new function () {
			var me = this;

			me.Debug = {
				LocalUrls: 1,
				UniqueUrls: 1,
			};

			if (url.match(/(localhost\:44).*\.htm/i) != null) {
				me.Debug = {
					LocalUrls: 1,
					UniqueUrls: 1,
				};
			}

			me.GetRoot = function () {
				return me.Debug.LocalUrls ? "https://localhost:44300/" : "https://user-web.googlecode.com/svn/trunk/";
			}

			me.Load = function () {
				me.Log("page url", url);
				me.LoadUserStyle();
			};

			me.LoadUserStyle = function () {

				var config = {
					bbc_news: { exp: /bbc\.co\.uk\/(news|sport|nature)/i },
					feedly: { exp: /\/\/feedly\.com/i },
					boing_boing: { exp: /boingxboing\.net/i },
					//gmail: { exp: /mail\.google\.com/i },
					google_maps: { exp: /maps\.google\.co/i },
					google_calendar: { exp: /google\.com\/calendar/i },
					google_search: { exp: /google\.(co\.uk|com)\/(search|webhp)/i },
					wikipedia: { exp: /wikipedia\.org/i },
					//you_tube: { exp: /youtube\.com/i },
					guardian: { exp: /(guardian\.co\.uk)|theguardian\.com/i },
					independent: { exp: /independent\.co\.uk/i },
					//private_eye: { exp: /private-eye\.co\.uk/i },
					old_reader: { exp: /theoldreader\.com/i },
					pocket: { exp: /getpocket\.com/i },
					stack_overflow: { exp: /stackoverflow\.com/i },
					test: { exp: /(testlocal|testlive|testinject)\.htm/i }
				};
                
                var found = false;

				for (var name in config) {
					var cfg = config[name];
					cfg.name = name;

					if (url.match(cfg.exp) != null) {
						me.Log("matched expression", cfg.exp);

						me.CurrentConfig = cfg;

						if (cfg.exp != "") {
                            found = true;
							me.LoadUserStyleSheet(name.replace(/_/ig, "-"));
							break;
						}
					}
				}
                
                if (!found) {
                    me.Log("no match", url);
                }
			};

			me.LoadUserStyleSheet = function (name) {
				var unique = me.Debug.UniqueUrls ? new Date().valueOf() : 1;
				var url = me.GetRoot() + "Styles/" + name + ".css?" + unique;

				me.Log("inserting style", url);

				var s = document.createElement('link');
				s.setAttribute('id', "user-web-css");
				s.setAttribute('href', url);
				s.setAttribute('rel', 'stylesheet');
				s.setAttribute('type', 'text/css');
				document.head.appendChild(s);
			};

			me.Log = function (text1, text2) {
				if (text2 != null) {
					if (text1.length < 20) {
						text1 = text1 + Array(21 - text1.length).join(" ");
					}

					console.log("[user-web]             " + text1 + ": " + text2);
				}
				else {
					console.log("[user-web]             " + text1);
				}
			};
		};

		window.setTimeout(function () {
			userWeb.Load();
		}, 100);
	}
	else  {
		console.log("[user-web]             skipping frame      : " + url);
	}
}
catch (e) {
    if (e.message.indexOf("accessing a cross-origin frame") > -1) {
    	console.log("[user-web-frame] " + e.message);
    }
    else {
     	console.log("[user-web] error: " + e.message);   
    }
}
